#!/usr/bin/python
# -*- coding: UTF-8 -*-
import requests
import os
from spider.requests_helper import Request
from lxml import html


def spider(page):
    """ 爬取meizitu网站的图片信息 """
    url = "https://www.doubiy.com/play/49054-1-{page}.html".format(page=page)

    # 获得html内容
    html_text = Request().get_html_text(url)

    # 获得xpath对象
    selector = html.fromstring(html_text)

    info_list = selector.xpath('//div[@class="info clearfix"]/script')

    script = info_list[0].text
    m3u8_url = script.split("var")[4].split('"')[1].replace("%3A", ":").replace("%2F", "/")
    return m3u8_url


if __name__ == '__main__':
    start_page = 903
    end_page = 928
    file = open('G:/海贼王视频下载地址.txt', 'w')
    for i in range(start_page, end_page):
        file.write(spider(i) + "\n")
        page = str((i + 1))
        file.write(page + "\n")
        print("下载{page}集成功, 地址: https://www.doubiy.com/play/49054-1-{i}.html".format(page=page, i=i))

    file.close()
